<template>
  <div class="top">
    <el-date-picker
      v-model="createTime"
      type="daterange"
      align="right"
      unlink-panels
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      :picker-options="pickerOptions"
      @change="changeTimes"
    />
    <el-button
      class="top-but"
      type="primary"
      icon="el-icon-search"
      @click="subOrderList"
    >查询</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      createTime: [],
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一天',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 1)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近两天',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 2)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三天',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 3)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      }
    }
  },
  created() {
    this.$store.dispatch('order/getCreatOrderTime').then(time => {
      this.createTime = time
    })
  },
  methods: {
    changeTimes() {
      this.$store.dispatch('order/setCreatOrderTime', this.createTime)
    },
    subOrderList() {
      this.$emit('getOrder', {
        creatTimes: this.getcreatTime()
      })
    },
    getcreatTime() {
      if (this.createTime === null) {
        return ''
      }
      if (Number.isInteger(this.createTime[0])) {
        return this.createTime
      }
      return this.createTime.map((item) => { return item.getTime() })
    }
  }
}
</script>

<style lang="scss" scoped>
.top {
  &-but {
    margin-left: 8px;
  }
}
</style>
